package org.greatstorm.servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class AdminCheckFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		UserService serv = UserServiceFactory.getUserService();

		if (serv.isUserLoggedIn() && serv.isUserAdmin()) {
			chain.doFilter(req, resp);
		} else {
			RequestDispatcher rd = req
					.getRequestDispatcher(serv
							.createLoginURL(((HttpServletRequest) req)
									.getRequestURI()));
			rd.forward(req, resp);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}

}
